var StrImg64 = '';
var socket = '';
var _atObjID = '';
var _atObjName = '';
var _msgID = '';
var _appID = G_Cookie.getAppID();
var _guid = G_Cookie.getGuid();
G_Prg.$('chatRoomContentID').style.display = 'block';
G_Prg.$('loadingDiv').style.display = "none";
_joinAskAnswer();
var content = '';
$('#contentBoxID').bind('input propertychange', function() {
	if($('#contentBoxID').val().length >= 1) {
		$('.sendButton').css({
			'border-radius': '4px',
			'background-color': '#FF6B6B',
			'color': '#fff'
		})
	} else {

		$('.sendButton').css({
			'border-radius': '4px',
			'background-color': '#F2F2F2',
			'color': '#B9B9B9'
		})
	}
});
/*******************************************************************************
 *功能：发送消息函数
 ******************************************************************************/
function _sendMsgClick(socket) {
	var content = G_Prg.$('contentBoxID').value;
	if(content == null) {
		if(_atObjName === '') {
			G_Prg.$('atBoxID').style.display = 'none';
			G_Prg.alert('请输入您想发送的信息');
		}
	} else {
		var contentFiltered = content.toString().trim().replace(/<.*>?<\/.*>|<.*\/>|\s+/g, '');
		if(_atObjID === '') {
			if(contentFiltered == "") {
				G_Prg.$('atBoxID').style.display = 'none';
				G_Prg.alert('请输入您想发送的信息');
			} else {
				socket.emit('message', {
						msg: content
					},
					function(err) {
						if(err) {
							var yesCallback = function() {
								$('.wrap').scrollTop($('.chatRoomContent_box').height());

							}
							G_Prg.alert(err, yesCallback);
							G_Prg.$('atBoxID').style.display = 'none';
						} else {
							G_Prg.$('contentBoxID').value = '';
							G_Prg.$('atBoxID').style.display = 'none';
						}
					});
			}
		} else {
			socket.emit('message', {
					msg: content,
					at: {
						userID: _atObjID,
						username: _atObjName
					}
				},
				function(err) {
					if(err) {
						var yesCallback = function() {
							$('.wrap').scrollTop($('.chatRoomContent_box').height());

						}
						G_Prg.alert(err, yesCallback);
					} else {
						G_Prg.$('contentBoxID').value = '';
						G_Prg.$('atBoxID').style.display = 'none';
					}
				});
		}
	}
}
/****************************************************************************
 * 功能：初始化
 ****************************************************************************/
function _joinAskAnswer() {
	//console.log(2)
	socket = io.connect('ws://apichat.ksbao.com:8080');
	$('#loadingDiv').css('display', 'block');
//	socket = io.connect('ws://192.168.5.21:8080');
	var isConnected = false;
	socket.on('connect',
			
		function(err) {
			if(err) {
				G_Prg.alert(err);
			} else {
				isConnected = true;
				//G_userActionMaiDian.chatroomnumbersCount();
				_canJoinAskAnswer(socket);
			}
		});
	socket.on('error',
		function(err) {});
	socket.on('disconnect',
		function() {
			isConnected = false;
		});
	G_Prg.$('sendButtonID').onclick = function() {
		_sendMsgClick(socket);
	}
	_listenOthersSendMsg(socket);
	_listenOthersJoinRoom(socket);
	_listenOthersLeaveRoom(socket);
	//与我相关=================================================
	G_Prg.$('myAskAnswer').onclick = function() {

		$('strong').css('display', 'none');
		$('.topBigBox').css('display', 'none');
		$('.chatRoomContent').css('display', 'none');
		$('.sendDiv').css('display', 'none');
		$('.more').css('display', 'none');
		$('.wrap').css('background', '#fff');
		$('#aboutMyChat_mainID').css('display', 'block');
		$('.msgNum').css('display', 'none');
		$('.dayiHeader h2').text('与我相关');
		_myAskAnswerClick(socket);
	};
	var fromUrl = G_Prg.getQueryString('fromUrl');
	if(fromUrl === 'rankList'){
		  var userid = G_Prg.getQueryString('userID');
		  var username = G_Prg.getQueryString('username');
		  _atObjID = parseInt(userid);
		  _atObjName = username;
		  G_Prg.$('aiTeButtonID').innerHTML = '@' + _atObjName;
		  G_Prg.$('aiTeButtonID').style.fontSize = '12px';
	  }
}
/***********************************************************************
 *点击与我相关后
 ***********************************************************************/
function _myAskAnswerClick(socket) {
	$('.wrap').scrollTop($('.chatRoomContent_box').height());
	_atMyMsgList(socket);
}
/**********************************************************************************
 * 未读消息数
 * *******************************************************************************/
function _listenNoReadMsg(socket) {
	socket.emit('at_not_read_count',
		function(err, notReadCount) {
			if(err) {
				G_Prg.alert(err);
			} else {
				if(notReadCount > 0) {
					G_Prg.$('askAnswerMsgNum').style.display = 'block';
					G_Prg.$('askAnswerMsgNum').innerHTML = notReadCount;
				} else {
					G_Prg.$('askAnswerMsgNum').style.display = 'none';
					G_Prg.$('askAnswerMsgNum').innerHTML = '';
				}
			}
		});
}
/****************************************************************************
 * 功能：参与聊天的条件
 * 参数：当前userID和当前guid
 ****************************************************************************/
function _canJoinAskAnswer(socket) {
	var _appID = G_Cookie.getAppID();
	var _guid = G_Cookie.getGuid();

	if(!G_Cookie.getUserID()) {
		var yesCallback = function() {
			window.location.href = 'userLogin.html?fromUrl=default.html&toUrl=default.html';
		}
		G_Prg.confirm('该功能登录后才能使用！', yesCallback);
		return;
	}
	socket.emit('join', {
			appID: _appID,
			guid: _guid,
			appType: 'danpin'
		},
		function(err, data) {
			if(err) {
				G_Prg.alert(err);
			} else {
				if(data.notReadCount > 0) {
					//与我相关的数量
					G_Prg.$('askAnswerMsgNum').style.display = 'block';
					G_Prg.$('askAnswerMsgNum').innerHTML = data.notReadCount;
				} else {

					G_Prg.$('askAnswerMsgNum').style.display = 'none';
					G_Prg.$('askAnswerMsgNum').innerHTML = '';
				}
				_showLatestMsg(data.msgList);

				_showChatUserList(data.roomUsers)
			}
		});
}
/****************************************************************************
 * 功能：展示最新的消息（当前用户发送的消息）
 ****************************************************************************/
function _showLatestMsg(msgListArr) {
	var latestMsgDiv = G_Prg.$('chatRoomContent_boxID').getElementsByTagName("div");
	var latestMsgDivLen = latestMsgDiv.length;
	for(var num2 = latestMsgDivLen - 1; num2 >= 0; num2--) {
		latestMsgDiv[num2].innerHTML = '';
		//G_Prg.$('chatRoomContent_boxID').removeChild(latestMsgDiv[num2]);
	}
	var curUserID = G_Cookie.getUserID();

	var msgListArrLen = msgListArr.length;
	for(var i = 0; i < msgListArrLen; i++) {
		var arrUserID = msgListArr[i].user.userID;
		if(parseInt(arrUserID) === parseInt(curUserID)) {
			var divTag = document.createElement('div');
			divTag.className = 'aui-chat-sender';
			var divTag1 = document.createElement('div');
			divTag1.className = 'myDiv';
			divTag1.innerHTML = '我';
			divTag.appendChild(divTag1);
			var divTag2 = document.createElement('div');
			divTag2.className = 'aui-chat-sender-cont myChatCont';
			divTag.appendChild(divTag2);
			var spanTag = document.createElement('span');
			if(msgListArr[i].type == 'file') {
				if(!msgListArr[i].at) {
					spanTag.innerHTML = '<img class="getPict" src="' + msgListArr[i].msg + '">';
				} else {
					spanTag.innerHTML = '<span class="atNameSpan">' + '@' + msgListArr[i].at.username + '</span></br><img class="getPict" src="' + msgListArr[i].msg + '">';
				}
			} else if(msgListArr[i].type == 'msg') {
				if(!msgListArr[i].at) {
					spanTag.innerHTML = msgListArr[i].msg;
				} else {
					spanTag.innerHTML = '<span class="atNameSpan">' + '@' + msgListArr[i].at.username + '</span>' + msgListArr[i].msg;
				}
			}

			divTag2.appendChild(spanTag);

			G_Prg.$('chatRoomContent_boxID').appendChild(divTag);
			$('.wrap').scrollTop($('.chatRoomContent_box').height());
		} else {
			var divOtherTag = document.createElement('div');
			divOtherTag.className = 'aui-chat-receiver';
			var divOtherTag1 = document.createElement('div');
			if(!msgListArr[i].user.type) {
				divOtherTag1.className = 'otherUserDiv';
			} else {
				if(msgListArr[i].user.type === 'zj') {
					divOtherTag1.className = 'zjUserDiv';
				} else {
					divOtherTag1.className = 'otherUserDiv';
				}
			}
			var usern = unescape(msgListArr[i].user.username);
			divOtherTag1.innerHTML = usern;
			divOtherTag.appendChild(divOtherTag1);
			//点击聊天框中的某人进行艾特对话
			(function(n) {
				divOtherTag1.onclick = function() {
					console.log(msgListArr[n].msg)
					_atObjID = msgListArr[n].user.userID;
					_atObjName = unescape(msgListArr[n].user.username);
					G_Prg.$('aiTeButtonID').innerHTML = '@' + _atObjName;
					G_Prg.$('aiTeButtonID').style.fontSize = '12px';
				}
			})(i);
			var divOtherTag2 = document.createElement('div');
			divOtherTag.appendChild(divOtherTag2);
			var divOtherTag3 = document.createElement('div');
			if(!msgListArr[i].user.type) {
				divOtherTag2.className = 'aui-chat-receiver-cont othersChatCont';
			} else {
				if(msgListArr[i].user.type === 'zj') {
					divOtherTag2.className = 'aui-chat-zjreceiver-cont othersChatCont';
				} else {
					divOtherTag2.className = 'aui-chat-receiver-cont othersChatCont';
				}
			}
			divOtherTag2.appendChild(divOtherTag3);
			var spanOhterTag = document.createElement('span');
			spanOhterTag.innerHTML = msgListArr[i].msg;
			if(msgListArr[i].type == 'file') {
				if(!msgListArr[i].at) {
					spanOhterTag.innerHTML = '<img class="getPict" src="' + msgListArr[i].msg + '"/>';
				} else {
					spanOhterTag.innerHTML = '<span class="atNameSpan">' + '@' + msgListArr[i].at.username + '</span></br><img class="getPict" src="' + msgListArr[i].msg + '"/>';
				}

			} else if(msgListArr[i].type == 'msg') {
				if(!msgListArr[i].at) {
					spanOhterTag.innerHTML = msgListArr[i].msg;
				} else {
					spanOhterTag.innerHTML = '<span class="atNameSpan">' + '@' + msgListArr[i].at.username + '</span>' + msgListArr[i].msg;
				}
			}

			divOtherTag2.appendChild(spanOhterTag);

			G_Prg.$('chatRoomContent_boxID').appendChild(divOtherTag);
			$('.wrap').scrollTop($('.chatRoomContent_box').height());

		}
	}
}
/****************************************************************************
 * 功能：监听别人的对话
 ****************************************************************************/
function _listenOthersSendMsg(socket) {
	socket.on('message',
		function(info) {
			if(info.at) {
				var atUserID = parseInt(info.at.userID);
				var curUserID = parseInt(G_Cookie.getUserID());
				if(atUserID === curUserID) {
					_listenNoReadMsg(socket);
				}
			}
			$('#loadingDiv').css('display', 'none');
			_showChatContent(info);
		});
}
/****************************************************************************
 * 功能：监听加入聊天室的人
 ****************************************************************************/
function _listenOthersJoinRoom(socket) {
	socket.on('join',
		function(info) {
			var divTag = document.createElement('div');
			divTag.className = 'aui-chat-sender';
			var divTag1 = document.createElement('div');
			divTag1.className = 'someonein';
			if(!info.user.type) {
				var ptUsern = unescape(info.user.username)
				divTag1.innerHTML = '<span class="ptUserIn">' + ptUsern + '</span>' + '<span class="chatRoomText">' + '进入了聊天室' + '</span>';
			} else {
				if(info.user.type === 'zj') {
					divTag1.innerHTML = '<span class="chatRoomText">' + '(专家)' + '</span>' + '<span class="zjUserIn">' + info.user.username + '</span>' + '<span class="chatRoomText">' + '进入了聊天室' + '</span>';
				} else {
					divTag1.innerHTML = '<span class="ptUserIn">' + info.user.username + '</span>' + '<span class="chatRoomText">' + '进入了聊天室' + '</span>';
				}
			}
			divTag.appendChild(divTag1);
			G_Prg.$('chatRoomContent_boxID').appendChild(divTag);
			$('.wrap').scrollTop($('.chatRoomContent_box').height());
			_showChatUserList(info.roomUsers);

		});

}
/****************************************************************************
 * 功能：监听离线的人
 ****************************************************************************/
function _listenOthersLeaveRoom(socket) {
	socket.on('leave',
		function(info) {
			_showChatUserList(info.roomUsers);
		});
}
/****************************************************************************
 * 功能：与我相关的socket连接 （每页只展示50条）
 ****************************************************************************/
function _atMyMsgList(socket) {
	socket.emit('at_all_list', {
			currentPage: 1,
			eachPageSize: 50
		},
		function(err, array) {
			if(err) {
				G_Prg.alert(err);
			} else {
				_showAtMyMsgList(array);
			}
		});
}
/**********************************************************************************
 * 与我相关的回复讨论区
 * *******************************************************************************/
function _showAtMyMsgList(msgArr) {
	var msgArrLen = msgArr.length;
	if(msgArrLen > 0) {
		G_Prg.$('noMyMsgID').style.display = 'none';
		var huifuStr = '';
		for(var i = 0; i < msgArrLen; i++) {
			var talkTimeyear = G_Prg.datetimeFormat(new Date(msgArr[i].msgTime), 'yyyy-MM-dd');
			var talkTimehour = G_Prg.datetimeFormat(new Date(msgArr[i].msgTime), 'hh:mm');
			if(msgArr[i].type == 'file') {
				//如果toUserID是当前用户，那么就是有消息，fromUserID就是谁艾特的我
				if(msgArr[i].toUserID == G_Cookie.getUserID()) {
					huifuStr = '<li><div class="dataInfo"><i></i>' + talkTimeyear + '<i></i></div>' +
						'<div id="otherUserInfoDivID" class="otherUserInfoDiv">' +
						'<span class="otherSpan">' + msgArr[i].fromUsername + '@我</span><span class="zhunTime">' + talkTimehour + '</span></br><img class="getpicC" src="' + msgArr[i].content + '">' +
						'</div><div class="replyAnswerDiv" id="' + msgArr[i].fromUsername + '">回复</div></li>';
					$('#myChatListID').append(huifuStr);
					$('.noMyMsg').css('display', 'none');
				} else {
					huifuStr = '<li><div class="dataInfo"><i></i>' + talkTimeyear + '<i></i></div>' +
						'<div id="otherUserInfoDivID" class="otherUserInfoDiv">' +
						'<span class="otherSpan">我@' + msgArr[i].toUsername + '</span><span class="zhunTime">' + talkTimehour + '</span></br><img class="getpicC" src="' + msgArr[i].content + '">' +
						'</div></li>';
					$('#myChatListID').append(huifuStr);
					$('.noMyMsg').css('display', 'none');

				}
			} else if(msgArr[i].type == 'msg'){
				//如果toUserID是当前用户，那么就是有消息，fromUserID就是谁艾特的我
				if(msgArr[i].toUserID == G_Cookie.getUserID()) {
					$('.noMyMsg').css('display', 'none');
						var talkTimehour = G_Prg.datetimeFormat(new Date(msgArr[i].msgTime), 'hh:mm');
						huifuStr = '<li><div class="dataInfo"><i></i>' + talkTimeyear + '<i></i></div>' +
							'<div id="otherUserInfoDivID" class="otherUserInfoDiv">' +
							'<span class="otherSpan">' + msgArr[i].fromUsername + '@我</span><span class="zhunTime">' + talkTimehour + '</span></br><span class="huifuC">' + msgArr[i].content + '</span>' +
							'</div><div class="replyAnswerDiv" id="' + msgArr[i].fromUsername + '">回复</div></li>';
						$('#myChatListID').append(huifuStr);
				} else {
					var talkTimehour = G_Prg.datetimeFormat(new Date(msgArr[i].msgTime), 'hh:mm');
						huifuStr = '<li><div class="dataInfo"><i></i>' + talkTimeyear + '<i></i></div>' +
							'<div id="otherUserInfoDivID" class="otherUserInfoDiv">' +
							'<span class="otherSpan">我@' + msgArr[i].toUsername + '</span><span class="zhunTime">' + talkTimehour + '</span></br><span class="huifuC">' + msgArr[i].content + '</span>' +
							'</div></li>';
						$('#myChatListID').append(huifuStr);

				}
			}

		}
		$('.replyAnswerDiv').on('click', function() {
			var n = $(this).index();
			var huifuN = this.id;
			_replyAnswerClick(msgArr[n].fromUserID, huifuN, msgArr[n].msgID);
		})

	}else {
		G_Prg.$('noMyMsgID').style.display = 'none';
	}
}
/**********************************************************************************
 * 聊天框进去后展示的10条信息
 * *******************************************************************************/
function _showChatContent(chatInfos) {
	$('#loadingDiv').css('display', 'block');
	if(chatInfos.self === true) {
		var divTag = document.createElement('div');
		divTag.className = 'aui-chat-sender';
		var divTag1 = document.createElement('div');
		divTag1.className = 'myDiv';
		divTag1.innerHTML = '我';
		divTag.appendChild(divTag1);
		var divTag2 = document.createElement('div');
		divTag2.className = 'aui-chat-sender-cont myChatCont';
		divTag.appendChild(divTag2);
		var spanTag = document.createElement('span');
		if(chatInfos.type == 'file') {
			if(!chatInfos.at) {
				spanTag.innerHTML = '<img class="getPict" src="' + chatInfos.msg + '">';
			} else {
				spanTag.innerHTML = '<span class="atNameSpan">' + '@' + chatInfos.at.username + '</span></br><img class="getPict" src="' + chatInfos.msg + '"/>';
			}

		} else {
			if(!chatInfos.at) {
				spanTag.innerHTML = chatInfos.msg;
			} else {
				spanTag.innerHTML = '<span class="atNameSpan">' + '@' + chatInfos.at.username + '</span>' + chatInfos.msg;
			}
		}

		divTag2.appendChild(spanTag);
		//		(function(n) {
		//			console.log(n)
		//			$('.getPict').click(function() {
		//				console.log('+++++')
		//			})
		//		})(i);
		G_Prg.$('chatRoomContent_boxID').appendChild(divTag);
		$('.wrap').scrollTop($('.chatRoomContent_box').height());
	} else {
		var divOtherTag = document.createElement('div');
		divOtherTag.className = 'aui-chat-receiver';
		var divOtherTag1 = document.createElement('div');
		if(!chatInfos.user.type) {
			divOtherTag1.className = 'otherUserDiv';
		} else {
			if(chatInfos.user.type === 'zj') {
				divOtherTag1.className = 'zjUserDiv';
			} else {
				divOtherTag1.className = 'otherUserDiv';
			}
		}
		divOtherTag1.innerHTML = chatInfos.user.username;
		divOtherTag.appendChild(divOtherTag1);
		divOtherTag1.onclick = function() {
			_atObjID = chatInfos.user.userID;
			_atObjName = chatInfos.user.username;
			G_Prg.$('aiTeButtonID').innerHTML = '@' + _atObjName;
			G_Prg.$('aiTeButtonID').style.fontSize = '12px';
			$('.atBox').css('display', 'none');
		}

		var divOtherTag2 = document.createElement('div');
		divOtherTag.appendChild(divOtherTag2);
		var divOtherTag3 = document.createElement('div');
		if(!chatInfos.user.type) {
			divOtherTag2.className = 'aui-chat-receiver-cont othersChatCont';
		} else {
			if(chatInfos.user.type === 'zj') {
				divOtherTag2.className = 'aui-chat-zjreceiver-cont othersChatCont';
			} else {
				divOtherTag2.className = 'aui-chat-receiver-cont othersChatCont';
			}
		}
		divOtherTag2.appendChild(divOtherTag3);
		var spanOhterTag = document.createElement('span');
		spanOhterTag.innerHTML = chatInfos.msg;
		if(chatInfos.type == 'file') {
			spanOhterTag.innerHTML = '<img class="getPict" src="' + chatInfos.msg + '">';
		} else {
			if(!chatInfos.at) {
				spanOhterTag.innerHTML = chatInfos.msg;
			} else {
				spanOhterTag.innerHTML = '<span class="atNameSpan">' + '@' + chatInfos.at.username + '</span>' + chatInfos.msg;
			}
		}
		//		if(!chatInfos.at) {
		//			
		//			if(info.type == 'file'){
		//				spanOhterTag.innerHTML = '<img class="getPict" src="' + chatInfos.msg + '">';
		//			}else {
		//				spanOhterTag.innerHTML = chatInfos.msg;
		//			}
		//		}else{
		//				spanOhterTag.innerHTML = chatInfos.msg;
		//		} 

		divOtherTag2.appendChild(spanOhterTag);
		G_Prg.$('chatRoomContent_boxID').appendChild(divOtherTag);
		$('.wrap').scrollTop($('.chatRoomContent_box').height());

	}
	$('#loadingDiv').css('display', 'none');
}
/******************************************************************************
 *1.展示可以at的成员列表
 * 2.展示本科目的老师列表
 * ****************************************************************************/
function _showChatUserList(roomUsersArr) {
	_showAtMembersList(roomUsersArr);
	_showMembersList(roomUsersArr);
	$('#loadingDiv').css('display', 'none');
}
/*********************************************************************
 * at框全部的专家 
 ********************************************************************/
function _showAtMembersList(roomUsersArr) {

	var allZhuanListLi = G_Prg.$('atZhuanListID').getElementsByTagName("li");
	var allZhuanListLiLen = allZhuanListLi.length;
	for(var num2 = allZhuanListLiLen - 1; num2 >= 0; num2--) {
		allZhuanListLi[num2].innerHTML = '';
		G_Prg.$('atZhuanListID').removeChild(allZhuanListLi[num2]);
	}
	var allUserListLi = G_Prg.$('atUserListID').getElementsByTagName("li");
	var allUserListLiLen = allUserListLi.length;
	for(var num1 = allUserListLiLen - 1; num1 >= 0; num1--) {
		allUserListLi[num1].innerHTML = '';
		G_Prg.$('atUserListID').removeChild(allUserListLi[num1]);
	}
//	var quxiaoStr = '<li class="atUserLiC" id="atMeLiC"><span>取消@</span></li>';
//	$('#cancelAtListID').append(quxiaoStr);
	$('#atMeLiC').on('click',function() {
			G_Prg.$('aiTeButtonID').innerHTML = '@';
			G_Prg.$('aiTeButtonID').style.fontSize = '22px';
			G_Prg.$('atBoxID').style.display = 'none';
			_atObjID = '';
			_atObjName = '';
	})
	var roomUsersArrLen = roomUsersArr.length;
	var curUserID = G_Cookie.getUserID();
	for(var i = 0; i < roomUsersArrLen; i++) {
		var arrUserID = roomUsersArr[i].userID;
		if(parseInt(arrUserID) !== parseInt(curUserID)) {
			var isExperts = roomUsersArr[i].type;
			if(isExperts === 'zj') {
				var liTag = document.createElement('li');
				liTag.className = 'atZhuanLi';
				liTag.id = 'atZhuanLi_' + i;
				if(parseInt(arrUserID) !== parseInt(curUserID)) {
					(function(n) {
						liTag.onclick = function() {
							_atZhuanLiClick(roomUsersArr[n].userID, roomUsersArr[n].username);
						}
					})(i);
				}
				var span1 = document.createElement('span');
				if(!roomUsersArr[i].joinTime) {
					span1.className = 'atZhuanLxSpan';
				} else {
					span1.className = 'atZhuanSpan';
				}
				span1.innerHTML = '专';
				liTag.appendChild(span1);
				var span2 = document.createElement('span');
				span2.className = 'atZhuanNameSpan';
				var usern = unescape(roomUsersArr[i].username);
				span2.innerHTML = usern;
				liTag.appendChild(span2);
				G_Prg.$('atZhuanListID').appendChild(liTag);
			} else {
				var liUserTag = document.createElement('li');
				liUserTag.className = 'atUserLi';
				liUserTag.id = 'atUserLi' + i;
				if(parseInt(arrUserID) !== parseInt(curUserID)) {
					(function(n) {
						liUserTag.onclick = function() {
							console.log('艾特同学')
							_atZhuanLiClick(roomUsersArr[n].userID, unescape(roomUsersArr[n].username));
						}
					})(i);
				}
				var spanTag = document.createElement('span');
				var usern = unescape(roomUsersArr[i].username);
				spanTag.innerHTML = usern;
				liUserTag.appendChild(spanTag);
				G_Prg.$('atUserListID').appendChild(liUserTag);
			}
		}
	}
}
/****************************************************************************
 * 功能：艾特专家时的对话框变化
 ****************************************************************************/
function _atZhuanLiClick(zjID, zjName) {
	G_Prg.$('atBoxID').style.display = 'none';
	_atObjID = zjID;
	_atObjName = zjName;
	G_Prg.$('aiTeButtonID').innerHTML = '@' + _atObjName;
	G_Prg.$('aiTeButtonID').style.fontSize = '12px';
	G_Prg.$('contentBoxID').addEventListener('input', function() {
		if(this.value.length >= 1) {
			$('.sendButton').css({
				'border-radius': '4px',
				'background-color': '#FF6B6B',
				'color': '#fff'
			})
		} else {
			$('.sendButton').css({
				'border-radius': '4px',
				'background-color': '#F2F2F2',
				'color': '#B9B9B9'
			})
		}
	}, false);
}
/****************************************************************************
 * 功能：展示轮播的专家列表 （在线的）
 ****************************************************************************/
function _showMembersList(roomUsersArr) {
	var allZhuanListLi = G_Prg.$('allZhuanID').getElementsByTagName("li");
	var allZhuanListLiLen = allZhuanListLi.length;
	for(var num2 = allZhuanListLiLen - 1; num2 >= 0; num2--) {
		allZhuanListLi[num2].innerHTML = '';
		G_Prg.$('allZhuanID').removeChild(allZhuanListLi[num2]);
	}
	var curUserID = G_Cookie.getUserID();
	var roomUsersArrLen = roomUsersArr.length;
	var zhuanStr = '';
	for(var i = 0; i < roomUsersArrLen; i++) {
		var arrUserID = roomUsersArr[i].userID;
		var isExperts = roomUsersArr[i].type;
		if(isExperts === 'zj') {
			zhuanStr = '<li class="zhuanList"  id ="' + arrUserID + '"><div class="memberNameDiv"><div class="aimeUserInfos"><div class="zhuanLxSpan fl"></div>' +
				'<div class="fl" style="margin-left:5%"><span class="memberName" id="memberNameID" >' + roomUsersArr[i].username + '</span><br />' +
				'<span class="zwSpan"><img src="/images/img/idcard.png" alt="" />' + roomUsersArr[i].info.zc + '-' + roomUsersArr[i].info.description + '</span><br />' +
				'<span class="onlinetime">专家在线时间：20:00-21:00</span><br /></div></div><div class="answerButton">' +
				'<span class="tiwenSpan">提问</span></div></div></li>';
			$('#allZhuanID').append(zhuanStr);
		}
	}
	var copyzhuan = '<li class="zhuanList"  id="' + roomUsersArr[0].userID + '"><div class="memberNameDiv"><div class="aimeUserInfos"><div class="zhuanLxSpan fl"></div>' +
		'<div class="fl" style="margin-left:5%"><span class="memberName" id="memberNameID">' + roomUsersArr[0].username + '</span><br />' +
		'<span class="zwSpan"><img src="/images/img/idcard.png" alt="" />' + roomUsersArr[0].info.zc + '-' + roomUsersArr[0].info.description + '</span><br />' +
		'<span class="onlinetime">专家在线时间：20:00-21:00</span><br /></div></div><div class="answerButton">' +
		'<span class="tiwenSpan">提问</span></div></div></li>';
	$('#allZhuanID').append(copyzhuan);
	var liTag = $('#allZhuanID li');
	$('#allZhuanID li').on('click', function() {
		var username = $(this).find('.memberName').text();
		var userid = $(this).attr('id')
		_askButtonClick(userid, username);
	})
}
/****************************************************************************
 * 功能：与我相关（点击提问老师）
 ****************************************************************************/
function _askButtonClick(userID, userName) {
	_atObjID = userID;
	_atObjName = userName;
	G_Prg.$('atBoxID').style.display = 'none';
	G_Prg.$('aiTeButtonID').innerHTML = '@' + _atObjName;
	G_Prg.$('aiTeButtonID').style.fontSize = '12px';
	$('.wrap').scrollTop($('.chatRoomContent_box').height());
}
/****************************************************************************
 * 功能：回复别人消息
 ****************************************************************************/
function _replyAnswerClick(userID, userName, msgID) {
	_atObjID = userID;
	_atObjName = userName;
	_msgID = msgID;
	$('.topBigBox').css('display', 'block');
	$('.chatRoomContent').css('display', 'block');
	$('.sendDiv').css('display', 'block');
	$('.more').css('display', 'none');
	$('#aboutMyChat_mainID').css('display', 'none');
	$('#myAskAnswer').css('display','block');
	$('.dayiHeader h2').text('在线答疑');
	G_Prg.$('aiTeButtonID').innerHTML = '@' + _atObjName;
	G_Prg.$('aiTeButtonID').style.fontSize = '12px';
	$('.wrap').scrollTop($('.chatRoomContent_box').height());

}
/*******************************************************************************
 * 所有点击事件
 * ******************************************************************************/
//点击取消弹框
$('.dropClose').on('click', function() {
	$('.tanDrop').fadeOut('fast');

})
$('.problemBtn').on('click', function() {
	$('.tanDrop').fadeOut('fast');

})
$('#aiTeButtonID').on('click', function() {
	$('.atBox').toggle();

})
$('.backBtn').on('click', function() {
	if($('.dayiHeader h2').html() == '与我相关') {

		window.location.href = '/onlineFAQ?fromUrl=find&type=1';
	} else {
		window.location.href = '/find'
	}
})

/*******************************************************************************
 * 温馨提示拨打电话
 * ******************************************************************************/
$('#telephone').click(function() {
	var tel = $(this).attr('phone');
	var browser = {
		versions: function() {
			var u = navigator.userAgent,
				app = navigator.appVersion;
			return {
				android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器                
				iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否为iPhone或者QQHD浏览器                
				iPad: u.indexOf('iPad') > -1, //是否iPad          
			};
		}()
	};
	if(browser.versions.android) {
		window.android.call(tel);
	} else {
		window.location.href = 'tel://' + tel;
	}
});
/*********************************************************************************
 * 发送图片
 *********************************************************************************/
$('#getPicID').click(function() {
	var browser = {
		versions: function() {
			var u = navigator.userAgent,
				app = navigator.appVersion;
			return {
				android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器                
				iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否为iPhone或者QQHD浏览器                
				iPad: u.indexOf('iPad') > -1, //是否iPad          
			};
		}()
	};
	if(browser.versions.android) {
		window.image.selectImage(G_Cookie.getUserID());
	} else {

		parent.window.location.href = 'iospickimg://aaaa'
	}
});
/***************************************************************************
 * 功能：发送的图片
 ****************************************************************************/
function sendpic(StrImg64) {
	StrImg64 = 'http://yingedu.oss-cn-shenzhen.aliyuncs.com/' + StrImg64;
//	alert('StrImg64');
	_listenOthersSendImg(socket, StrImg64);
}
/***************************************************************************
 * 功能：监听发送的图片
 ****************************************************************************/
function _listenOthersSendImg(socket, StrImg64) {
	//emit发送
	socket.emit('message', {
			msg: StrImg64,
			type: 'file',
			at: {
				userID: _atObjID,
				username: _atObjName
			}
		},
		function(err, data) {
			if(err) {
				G_Prg.alert(err);
			} else {
//				if(data.notReadCount > 0) {
//					//与我相关的数量
//					G_Prg.$('askAnswerMsgNum').style.display = 'block';
//					G_Prg.$('askAnswerMsgNum').innerHTML = data.notReadCount;
//				} else {
//
//					G_Prg.$('askAnswerMsgNum').style.display = 'none';
//					G_Prg.$('askAnswerMsgNum').innerHTML = '';
//				}
//				_showLatestMsg(data.msgList);
//
//				_showChatUserList(data.roomUsers)
			}
		})
};